package com.nbx.app.zeuscmdport;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.view.KeyEvent;
import android.view.MotionEvent;

public class Render01 extends RenderBase {

	private int mColorType = 0;
	public Render01(Context context) {
		super(context);
	}


	public void onDrawFrame(GL10 gl) {
		handleClearColor(gl);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		float ratio = (float) width / height;
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		gl.glDisable(GL10.GL_DITHER);
		gl.glClearColor(1, 1, 1, 1);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glEnable(GL10.GL_DEPTH_TEST);
	}
	
	@Override
	public boolean handleKeyEvent(int keyCode, KeyEvent event) {
		if(mColorType < 6){
			mColorType ++;
		}
		return false;
	}

	@Override
	public boolean handleTouchEvent(MotionEvent event) {
		if(mColorType < 6){
			mColorType ++;
		}else{
			mColorType = 0;
		}
		return true;
	}		
	
	private void handleClearColor(GL10 gl){
		switch(mColorType){
			case 0: gl.glClearColor(0, 1, 1, 1); break;
			case 1: gl.glClearColor(1, 0, 1, 1); break;
			case 2: gl.glClearColor(1, 1, 0, 1); break;
			case 3: gl.glClearColor(0, 0, 1, 1); break;
			case 4: gl.glClearColor(0, 1, 0, 1); break;
			case 5: gl.glClearColor(1, 0, 0, 1); break;
			case 6: gl.glClearColor(0, 0, 0, 1); break;
		}
	}
}